package com.binvshe.binvshe.account;

import android.text.TextUtils;

import com.binvshe.binvshe.app.MyApplication;
import com.binvshe.binvshe.organization.entity.ClubEntity;
import com.google.gson.annotations.SerializedName;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

/**
 * 账号类
 * <p/>
 * 数据直接写到sp里，从内存中读取
 * <p/>
 * user_id : test
 * real_name : 真实姓名
 * sign : 签名
 * portrait :
 * phone : 18088888888
 * gender : WOMAN
 * company : 爬虫
 * department : IT事业部
 * position : null
 * ext_info :
 * dateline : 1468217954
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/7/12 16:35
 * @Version: 1.0.0
 */
public class Account {

    private static final String SP_FILENAME_ACCOUNT = Account.class.getSimpleName();
    private static final String KEY_REAL_NAME = "real_name";
    private static final String KEY_LOGIN_NAME = "login_name";
    private static final String KEY_APP_ID = "app_id";
    private static final String KEY_SIGN = "sign";
    private static final String KEY_PROTRAIT = "portrait";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_GENDER = "gender";
    private static final String KEY_ROLE = "role";
    private static final String KEY_LOCKED = "locked";
    private static final String KEY_CREATE_TIME = "create_time";
    private static final String KEY_UPDATE_TIME = "update_time";
    private static final String KEY_COMPANY = "company";
    private static final String KEY_POSITION = "position";
    private static final String KEY_NICK_NAME = "nick_name";
    private static final String KEY_COORDS = "coords";
    private static final String KEY_BIRTHDAY = "birthday";
    private static final String KEY_CONSTELLATION = "constellation";
    private static final String KEY_PROVINCE = "province";
    private static final String KEY_CITY = "city";
    private static final String KEY_COIN = "coin";
    private static final String KEY_TRADING_PWD = "trading_pwd";
    private static final String KEY_SALT = "salt";
    private static final String KEY_NAME_PY = "name_py";
    private static final String KEY_INITIAL = "initial";
    private static final String KEY_FANS = "fans";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_IN_CLUB = "in_club";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_FOLLOW_STATE = "follow_state";
    private static final String KEY_CHECK_IN_DAYS = "key_check_in_days";
    private static final String KEY_CLUB_ROLE = "key_club_role";


    @SerializedName("login_name")
    private String mLoginName;
    @SerializedName("real_name")
    private String mRealName;
    @SerializedName("app_id")
    private String mAppId;
    @SerializedName("sign")
    private String mSign;
    @SerializedName("portrait")
    private String mPortrait;
    @SerializedName("phone")
    private String mPhone;
    @SerializedName("gender")
    private int mGender;
    @SerializedName("role")
    private String mRole;
    @SerializedName("locked")
    private String mLocked;
    @SerializedName("create_time")
    private String mCreateTime;
    @SerializedName("update_time")
    private String mUpdateTime;
    @SerializedName("company")
    private String mCompany;
    @SerializedName("position")
    private String mPosition;
    @SerializedName("nick_name")
    private String mNickName;
    @SerializedName("coords")
    private String mCoords;
    @SerializedName("birthday")
    private String mBirthday;
    @SerializedName("constellation")
    private String mConstellation;
    @SerializedName("province")
    private String mProvince;
    @SerializedName("city")
    private String mCity;
    @SerializedName("coin")
    private int mCoin;
    @SerializedName("trading_pwd")
    private String mTrading_pwd;
    @SerializedName("salt")
    private String mSalt;
    @SerializedName("name_py")
    private String mName_py;
    @SerializedName("initial")
    private String mInitial;
    @SerializedName("fans")
    private int mFans;
    @SerializedName("user_id")
    private String mUserId;
    @SerializedName("in_club")
    private boolean mIn_club;
    @SerializedName("club")
    private ClubEntity mClub;
    @SerializedName("follow_state")
    private int mFollowState;
    @SerializedName("check_in_days")
    private int mCheckInDays;
    @SerializedName("club_role")
    private int mClubRole;
    private String friend;
    private String mPassWord;

    public Account() {
    }

    /**
     * 初始化，从SP中读取
     */
    public static Account createFromSp() {
        Account account = new Account();
        account.setRealName(getString(KEY_REAL_NAME));
        account.setLoginName(getString(KEY_LOGIN_NAME));
        account.setAppId(getString(KEY_APP_ID));
        account.setSign(getString(KEY_SIGN));
        account.setPortrait(getString(KEY_PROTRAIT));
        account.setPhone(getString(KEY_PHONE));
        account.setGender(getInt(KEY_GENDER));
        account.setRole(getString(KEY_ROLE));
        account.setLocked(getString(KEY_LOCKED));
        account.setCreateTime(getString(KEY_CREATE_TIME));
        account.setUpdateTime(getString(KEY_UPDATE_TIME));
        account.setCompany(getString(KEY_COMPANY));
        account.setPosition(getString(KEY_POSITION));
        account.setUserId(getString(KEY_USER_ID));
        account.setNickName(getString(KEY_NICK_NAME));
        account.setCoords(getString(KEY_COORDS));
        account.setBirthDay(getString(KEY_BIRTHDAY));
        account.setConstellation(getString(KEY_CONSTELLATION));
        account.setProvince(getString(KEY_PROVINCE));
        account.setCity(getString(KEY_CITY));
        account.setCoin(getInt(KEY_COIN));
        account.setTradingPwd(getString(KEY_TRADING_PWD));
        account.setSalt(getString(KEY_SALT));
        account.setNamePy(getString(KEY_NAME_PY));
        account.setInitial(getString(KEY_INITIAL));
        account.setFans(getInt(KEY_FANS));
        account.setInClub(getBoolean(KEY_IN_CLUB));
        account.setPassWord(getString(KEY_PASSWORD));
        account.setFollowState(getInt(KEY_FOLLOW_STATE));
        account.setCheckInDays(getInt(KEY_CHECK_IN_DAYS));
        account.setClubRole(getInt(KEY_CLUB_ROLE));
        account.setClub(ClubEntity.createFromSp());
        return account;
    }


    /**
     * 初始化, 克隆一个对象
     */
    public static Account createFrom(Account acct) {
        Account account = new Account();
        account.setRealName(acct.getRealName());
        account.setLoginName(acct.getLoginName());
        account.setAppId(acct.getAppId());
        account.setSign(acct.getSign());
        account.setPortrait(acct.getPortrait());
        account.setPhone(acct.getPhone());
        account.setGender(acct.getGender());
        account.setRole(acct.getRole());
        account.setLocked(acct.getLocked());
        account.setCreateTime(acct.getCreateTime());
        account.setUpdateTime(acct.getUpdateTime());
        account.setCompany(acct.getCompany());
        account.setPosition(acct.getPosition());
        account.setNickName(acct.getNickName());
        account.setCoords(acct.getCoords());
        account.setBirthDay(acct.getBirthDay());
        account.setConstellation(acct.getConstellation());
        account.setProvince(acct.getProvince());
        account.setCity(acct.getCity());
        account.setCoin(acct.getCoin());
        account.setTradingPwd(acct.getTradingPwd());
        account.setSalt(acct.getSalt());
        account.setNamePy(acct.getNamePy());
        account.setInitial(acct.getInitial());
        account.setFans(acct.getFans());
        account.setUserId(acct.getUserId());
        account.setInClub(acct.getInClub());
        account.setPassWord(acct.getPassWord());
        account.setClub(acct.getClub());
        account.setFollowState(acct.getFollowState());
        account.setCheckInDays(acct.getCheckInDays());
        account.setClubRole(acct.getClubRole());
        if (acct.getClub() != null) {
            ClubEntity.createFrom(acct.getClub());
        }
        return account;
    }


    public String getRealName() {
        return mRealName;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param realName
     */
    public void setRealName(String realName) {
        if (!TextUtils.equals(this.mRealName, realName)) {
            this.mRealName = realName;
            saveString(KEY_REAL_NAME, mRealName);
        }
    }

    public String getFriend() {
        return friend;
    }


    public void setFriend(String friend) {
        this.friend=friend;

    }

    public String getLoginName() {
        return mLoginName;
    }

    public void setLoginName(String loginName) {
        if (!TextUtils.equals(this.mLoginName, loginName)) {
            mLoginName = loginName;
            saveString(KEY_LOGIN_NAME, mLoginName);
        }
    }


    public String getAppId() {
        return mAppId;
    }

    public void setAppId(String appId) {
        if (!TextUtils.equals(this.mAppId, appId)) {
            mAppId = appId;
            saveString(KEY_APP_ID, mAppId);
        }
    }

    public String getSign() {
        return mSign;
    }

    public void setSign(String sign) {
        if (!TextUtils.equals(this.mSign, sign)) {
            mSign = sign;
            saveString(KEY_SIGN, mSign);
        }
    }

    public String getPortrait() {
        return mPortrait;
    }

    public void setPortrait(String portrait) {
        if (!TextUtils.equals(this.mPortrait, portrait)) {
            mPortrait = portrait;
            saveString(KEY_PROTRAIT, mPortrait);
        }
    }

    public String getPhone() {
        return mPhone;
    }

    public void setPhone(String phone) {
        if (!TextUtils.equals(this.mPhone, phone)) {
            mPhone = phone;
            saveString(KEY_PHONE, mPhone);
        }
    }

    public int getGender() {
        return mGender;
    }

    public void setGender(int gender) {
        if (!(mGender == gender)) {
            mGender = gender;
            saveInt(KEY_GENDER, mGender);
        }
    }

    public int getClubRole() {
        return mClubRole;
    }

    public void setClubRole(int clubRole) {
        if (!(mClubRole == clubRole)) {
            mClubRole = clubRole;
            saveInt(KEY_CLUB_ROLE, mClubRole);
        }
    }

    public String getUserId() {
        return mUserId;
    }

    public void setUserId(String userId) {
        if (!TextUtils.equals(this.mUserId, userId)) {
            mUserId = userId;
            saveString(KEY_USER_ID, mUserId);
        }
    }

    public String getRole() {
        return mRole;
    }

    public void setRole(String role) {
        if (!TextUtils.equals(this.mRole, role)) {
            mRole = role;
            saveString(KEY_ROLE, mRole);
        }
    }

    public String getLocked() {
        return mLocked;
    }

    public void setLocked(String locked) {
        if (!TextUtils.equals(this.mLocked, locked)) {
            mLocked = locked;
            saveString(KEY_LOCKED, mLocked);
        }
    }

    public String getCreateTime() {
        return mCreateTime;
    }

    public void setCreateTime(String createTime) {
        if (!TextUtils.equals(this.mCreateTime, createTime)) {
            mCreateTime = createTime;
            saveString(KEY_CREATE_TIME, mCreateTime);
        }
    }

    public String getUpdateTime() {
        return mUpdateTime;
    }

    public void setUpdateTime(String updateTime) {
        if (!TextUtils.equals(this.mUpdateTime, updateTime)) {
            mUpdateTime = updateTime;
            saveString(KEY_UPDATE_TIME, mUpdateTime);
        }
    }

    public String getCompany() {
        return mCompany;
    }

    public void setCompany(String company) {
        if (!TextUtils.equals(this.mCompany, company)) {
            mCompany = company;
            saveString(KEY_COMPANY, mCompany);
        }
    }

    public String getPosition() {
        return mPosition;
    }

    public void setPosition(String position) {
        if (!TextUtils.equals(this.mPosition, position)) {
            mPosition = position;
            saveString(KEY_POSITION, mPosition);
        }
    }

    public String getNickName() {
        return mNickName;
    }

    public void setNickName(String nickName) {
        if (!TextUtils.equals(this.mNickName, nickName)) {
            mNickName = nickName;
            saveString(KEY_NICK_NAME, mNickName);
        }
    }

    public String getCoords() {
        return mCoords;
    }

    public void setCoords(String coords) {
        if (!TextUtils.equals(this.mCoords, coords)) {
            mCoords = coords;
            saveString(KEY_COORDS, mCoords);
        }
    }

    public String getBirthDay() {
        return mBirthday;
    }

    public void setBirthDay(String birthDay) {
        if (!TextUtils.equals(this.mBirthday, birthDay)) {
            mBirthday = birthDay;
            saveString(KEY_BIRTHDAY, mBirthday);
        }
    }

    public String getConstellation() {
        return mConstellation;
    }

    public void setConstellation(String constellation) {
        if (!TextUtils.equals(this.mConstellation, constellation)) {
            mConstellation = constellation;
            saveString(KEY_CONSTELLATION, mConstellation);
        }
    }

    public String getProvince() {
        return mProvince;
    }

    public void setProvince(String province) {
        if (!TextUtils.equals(this.mProvince, province)) {
            mProvince = province;
            saveString(KEY_PROVINCE, mProvince);
        }
    }

    public String getCity() {
        return mCity;
    }

    public void setCity(String city) {
        if (!TextUtils.equals(this.mCity, city)) {
            mCity = city;
            saveString(KEY_CITY, mCity);
        }
    }

    public int getCoin() {
        return mCoin;
    }

    public void setCoin(int coin) {
        if (!(mCoin == coin)) {
            mCoin = coin;
            saveInt(KEY_COIN, mCoin);
        }
    }

    public int getCheckInDays() {
        return mCheckInDays;
    }

    public void setCheckInDays(int checkInDays) {
        if (!(mCheckInDays == checkInDays)) {
            mCheckInDays = checkInDays;
            saveInt(KEY_CHECK_IN_DAYS, mCheckInDays);
        }
    }

    public String getTradingPwd() {
        return mTrading_pwd;
    }

    public void setTradingPwd(String tradingPwd) {
        if (!TextUtils.equals(this.mTrading_pwd, tradingPwd)) {
            mTrading_pwd = tradingPwd;
            saveString(KEY_TRADING_PWD, mTrading_pwd);
        }
    }

    public String getSalt() {
        return mSalt;
    }

    public void setSalt(String salt) {
        if (!TextUtils.equals(this.mSalt, salt)) {
            mSalt = salt;
            saveString(KEY_SALT, mPosition);
        }
    }

    public String getNamePy() {
        return mName_py;
    }

    public void setNamePy(String namePy) {
        if (!TextUtils.equals(this.mName_py, namePy)) {
            mName_py = namePy;
            saveString(KEY_NAME_PY, mName_py);
        }
    }

    public String getPassWord() {
        return mPassWord;
    }

    public void setPassWord(String passWord) {
        if (!TextUtils.equals(this.mPassWord, passWord)) {
            mPassWord = passWord;
            saveString(KEY_PASSWORD, mPassWord);
        }
    }

    public String getInitial() {
        return mInitial;
    }

    public void setInitial(String initial) {
        if (!TextUtils.equals(this.mInitial, initial)) {
            mInitial = initial;
            saveString(KEY_INITIAL, mInitial);
        }
    }

    public int getFans() {
        return mFans;
    }

    public void setFans(int fans) {
        if (!(mFans == fans)) {
            mFans = fans;
            saveInt(KEY_FANS, mFans);
        }
    }

    public int getFollowState() {
        return mFollowState;
    }

    public void setFollowState(int followState) {
        if (mFollowState != followState) {
            mFollowState = followState;
            saveInt(KEY_FOLLOW_STATE, mFollowState);
        }
    }

    public boolean getInClub() {
        return mIn_club;
    }

    public void setInClub(boolean inClub) {
        if (!(mIn_club == inClub)) {
            mIn_club = inClub;
            saveBoolean(KEY_IN_CLUB, mIn_club);
        }
    }

    public ClubEntity getClub() {
        if (mClub == null || TextUtils.isEmpty(mClub.getName())) {
            return null;
        }
        return mClub;
    }

    public void setClub(ClubEntity club) {
        mClub = club;
    }

    @Override
    public String toString() {
        return "Account{" +
                "mLoginName='" + mLoginName + '\'' +
                ", mRealName='" + mRealName + '\'' +
                ", mAppId='" + mAppId + '\'' +
                ", mSign='" + mSign + '\'' +
                ", mPortrait='" + mPortrait + '\'' +
                ", mPhone='" + mPhone + '\'' +
                ", mGender=" + mGender +
                ", mRole='" + mRole + '\'' +
                ", mLocked='" + mLocked + '\'' +
                ", mCreateTime='" + mCreateTime + '\'' +
                ", mUpdateTime='" + mUpdateTime + '\'' +
                ", mCompany='" + mCompany + '\'' +
                ", mPosition='" + mPosition + '\'' +
                ", mNickName='" + mNickName + '\'' +
                ", mCoords='" + mCoords + '\'' +
                ", mBirthday='" + mBirthday + '\'' +
                ", mConstellation='" + mConstellation + '\'' +
                ", mProvince='" + mProvince + '\'' +
                ", mCity='" + mCity + '\'' +
                ", mCoin=" + mCoin +
                ", mTrading_pwd='" + mTrading_pwd + '\'' +
                ", mSalt='" + mSalt + '\'' +
                ", mName_py='" + mName_py + '\'' +
                ", mInitial='" + mInitial + '\'' +
                ", mFans=" + mFans +
                ", mUserId='" + mUserId + '\'' +
                ", mIn_club=" + mIn_club +
                ", mPassWord='" + mPassWord + '\'' +
                '}';
    }

    private static void saveInt(String key, int value) {
        SharePreferenceUtil.saveInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static int getInt(String key) {
        return SharePreferenceUtil.getInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    private static void saveString(String key, String value) {
        SharePreferenceUtil.saveString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static String getString(String key) {
        return SharePreferenceUtil.getString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    private static void saveBoolean(String key, Boolean value) {
        SharePreferenceUtil.saveBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static Boolean getBoolean(String key) {
        return SharePreferenceUtil.getBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, false);
    }

    public void clear() {
        SharePreferenceUtil.clear(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT);
    }
}
